package com.kgc.warehouse.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.entity.Permission;
import com.kgc.warehouse.system.mapper.PermissionMapper;
import com.kgc.warehouse.system.service.PermissionService;
import com.kgc.warehouse.system.vo.PermissionVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired(required = false)
    private PermissionService permissionService;

    @RequestMapping("list")
//    @RequiresPermissions("menu:view")
//    @RequiresPermissions("permission:view")
    public ResponseResult list()
    {
        List<Permission> list = permissionService.list();
        List<PermissionVO> collect = list.stream().map(permission -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return permissionVO;
        }).collect(Collectors.toList());
        return new ResponseResult(collect,null, CodeMsg.SUCCESS);
    }

    @RequestMapping("treeSelect")
    public ResponseResult treeSelect()
    {
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getType, "menu");
        List<Permission> list = permissionService.list(lambda);
        return new ResponseResult(list,null,CodeMsg.SUCCESS);
    }

    @RequestMapping("add")
    @RequiresPermissions("menu:create")
    public ResponseResult add(Permission permission)
    {

        return permissionService.add(permission);
    }

    @RequestMapping("selectById")
    public ResponseResult selectById(Integer id)
    {
        Permission byId = permissionService.getById(id);
        return new ResponseResult(byId,null,CodeMsg.SUCCESS);
    }

    @RequestMapping("update")
    @RequiresPermissions("menu:update")
    public ResponseResult update(Permission permission)
    {
        return permissionService.updatePermission(permission);
    }

    @RequestMapping("delete")
    @RequiresPermissions("menu:delete")
    public ResponseResult delete(Integer id)
    {
        return permissionService.delete(id);
    }
}
